package util;

import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class Functions {

	/**
	 * This method translate a text from French to English using the Microsoft
	 * Translator API Max number of characters to translate: 2.000.000/month
	 * (free version).
	 * 
	 * @param text
	 *            : String
	 * @throws Exception
	 */
	public static String translateFrenchToEnglish(String text) throws Exception {

		/*
		 * Several account because the free option is very limited
		 * 
		 * ClientSecret: nYDpz/7zieiWPxIWbFbXko+2TXldVTGOZxCZwSH8TwI=
		 * ClientId: ca16a138-a3c2-476c-bb7f-978f508b021e
		 * 
		 * ClientSecret: jC6q56oidVxA4Q1vbeU/3nX5Y3iHwujUo1b1pGBc+m8=
		 * ClientId: fragel
		 */
		
		/*
		 * ClientSecret:  	b+ew4XOX7bo3p3Ri/pDkmE73dcPyjUXaPKdnEHLLg6I=
		 * ClientId: 5c3894a9-7f1d-410c-b5e7-863116b11359
		 * name: Project
		 * apellidos: Brussels
		 */
		
		/*
		 * Client ID: 5c3894a9-7f1d-410c-b5e7-863116b11359
		 * ClientSecret: bX5PNYtpQNJIYN9UlwDDLuaf/bNayp4lF2MsA+Q3DNs=
		 */
		
		Translate.setClientId("5c3894a9-7f1d-410c-b5e7-863116b11359"); // my user Client ID
		Translate
				.setClientSecret("bX5PNYtpQNJIYN9UlwDDLuaf/bNayp4lF2MsA+Q3DNs="); // my
																					// "secret"Client
																					// pass
		String translatedText = Translate.execute(text, Language.FRENCH,
				Language.ENGLISH);
//		System.out.println("\n\nDESDE API: texto traducido: " + translatedText);
		return translatedText;
	}

	public static String getStringFromDocument(Document doc) {
		try {
			DOMSource domSource = new DOMSource(doc);
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);

			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.transform(domSource, result);

			return writer.toString();
		} catch (TransformerException ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * This method will return the name of the section parsed, for
	 * including to the Discipline object into the database
	 * @param request
	 * @return String with the name of the section parsed
	 */
	public static String getSectionParsed(HttpServletRequest request){
		String name = request.getParameter("section");
		String res = "";
		if(name.contains("cinema")){
			res="Cinéma";
		}
		else if(name.contains("litterature")){
			res="Littérature";
		}
		else if(name.contains("exposition")){
			res="Exposition";
		}
		else if(name.contains("scene")){
			res="Scène";
		}
		else if(name.contains("concours")){
			res="Concours";
		}
		else if(name.contains("galeries")){
			res="Galeries";
		}
		return res;
		
	}

}
